template.defaults.imports.getVideoInfo = function (videoId) {
    $.ajaxSettings.async = false;
    $.get('/listSnapshots',{
        videoId:videoId,
    },function(result){
        if (result.code == 100){
            var url = result.data.MediaSnapshot.Snapshots.Snapshot[0].Url;
            $.get('/getPlayInfo?videoId=' + videoId,function(result){
                var playVideo = '';
                for (var i = 0;i < result.data.length; i++){
                    if (result.data[i].Format == 'mp4'){
                        playVideo = result.data[i].PlayURL;
                        break;
                    }
                }
                template.defaults.imports.video = {
                    playUrl:playVideo,
                    imgUrl:url
                };
                $.ajaxSettings.async = true;
            });
        }
    });
};
/* global FormData visitUrl*/
/* global getCookie */
/* global template */
/* global proDetails */
/* global pro Aliplayer*/
var appearanceList = [];
var imgList = {};
var list = [];
var imgPath = '';
(function ($) {
    //一级分类
    $.ajax({
        type: 'POST',
        url: "/antique/api/sysDict/query_list",
        data: {
            type: "firstcategory",
            "token": getCookie('token')
        },
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                if (result.data && result.data.list && result.data.list.length > 0) {
                    var list = result.data.list;
                    for (var i = 0; i < list.length; i++) {
                        if (pro.firstCategory == list[i].id) {
                            $("#firstCategory").append("<option value='" + list[i].id + "' selected>" + list[i].label + "</option>");
                            selectSecondCategory(pro.firstCategory);
                        } else {
                            $("#firstCategory").append("<option value='" + list[i].id + "'>" + list[i].label + "</option>");
                        }
                    }
                }
            }
        }
    });
})(jQuery);

$("#firstCategory").change(function () {
    var value = $("#firstCategory").val();
    selectSecondCategory(value);
});

function selectSecondCategory(value) {
    $("#secondCategory").empty();
    $("#secondTags").html('');
    $.ajax({
        type: 'POST',
        url: "/antique/api/sysDict/query_list",
        data: {
            type: "secondcategory",
            "token": getCookie('token'),
            parentId: value
        },
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                if (result.data && result.data.list && result.data.list.length > 0) {
                    var list = result.data.list;
                    $("#secondCategory").append("<option value='' disabled selected style='display:none;'>请选择</option>>");
                    for (var i = 0; i < list.length; i++) {
                        if (pro.secondCategory == list[i].id) {
                            $("#secondCategory").append("<option value='" + list[i].id + "' selected>" + list[i].label + "</option>");
                        } else {
                            $("#secondCategory").append("<option value='" + list[i].id + "'>" + list[i].label + "</option>");
                        }
                    }
                }
            }
        }
    });
}

$("#secondCategory").change(function () {
    var value = $("#secondCategory").val();
    $.ajax({
        type: 'POST',
        url: "/antique/api/sysDict/query_lower_list",
        data: {
            parentId: value
        },
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                if (result.data) {
                    var list = result.data;
                    var html = template('secondTagsContent', list);
                    document.getElementById('secondTags').innerHTML = html;
                } else {
                    $("#secondTags").html('');
                }
            }
        }
    });
});

$("#detailsDesc").keyup(function () {
    var value = $.trim(this.value);
    var descLength = $.trim(this.value).length;
    if (descLength <= 200) {
        $("#descNumber").html(descLength);
    } else {
        $("#detailsDesc").val(value.substring(0, 200));
    }
});

var submiting = false;

function puilcPro(state) {
    if(submiting)return false;
    submiting = true;
    var tagSelect = $("#secondTags").find('select');
    var tags = '';
    tagSelect && tagSelect.each(function (index, element) {
        tags = tags + $(this).val() + ',';
    });
    if (tags) {
        tags = tags.substring(0, tags.length - 1);
    }
    var id = $("#proId").val();
    var name = $("#name").val();
    var price = $("#price").val();
    var sizeDesc = $("#sizeDesc").val();
    var inventory = $("#inventory").val();
    var firstCategory = $("#firstCategory").children('option:selected').val();
    var secondCategory = $("#secondCategory").children('option:selected').val();
    //var property = $("#property").data('values');
    //var appearance = $("#appearance").data('values');
    var detailsDesc = $("#detailsDesc").val();
    var showImages = $("#showImages").val();
    var showVideos = $('#showVideos').val();
    var logisticsFee = $('#logisticsFee').val();
    var isAgree = $('#weuiAgree').prop("checked");

    if (firstCategory == '') {
        $.toast("分类不能为空", "cancel");
        submiting = false;
        return;
    }
    if (secondCategory == '' || typeof secondCategory == 'undefined') {
        $.toast("分类不能为空", "cancel");
        submiting = false;
        return;
    }
    if (name == '') {
        $.toast("名称不能为空", "cancel");
        submiting = false;
        return;
    }
    if (sizeDesc == '') {
        $.toast("尺寸不能为空", "cancel");
        submiting = false;
        return;
    }
    if (detailsDesc == '') {
        $.toast("描述不能为空", "cancel");
        submiting = false;
        return;
    }
    if (typeof showImages == 'undefined' || showImages == '') {
        $.toast("请上传图片", "cancel");
        submiting = false;
        return;
    }

    if (price == '') {
        $.toast("价格不能为空", "cancel");
        submiting = false;
        return;
    }

    if (inventory == '' || inventory == 0 || inventory == '0') {
        $.toast("库存不能为空", "cancel");
        submiting = false;
        return;
    }
    
    if(!isAgree){
        $.toast("请同古今在线交易规则后才能成功上传藏品", "cancel");
        submiting = false;
        return;
    }
    var params = {};
    params.id = id;
    params.type = 2;
    params.name = name;
    params.price = price;
    params.sizeDesc = sizeDesc;
    params.inventory = inventory;
    params.firstCategory = firstCategory;
    params.secondCategory = secondCategory;
    params.logisticsFee = logisticsFee;
    params.property = 2;
    // params.appearance = appearance;
    params.detailsDesc = detailsDesc;
    params.showImages = showImages;
    if (showVideos && showVideos != '') {
        params.showVideos = showVideos;
    }
    params.tags = tags;
    params.token = getCookie('token');

    if (id) {
        upPro(params, state);
    } else {
        addPro(params, state);
    }
}

function addPro(params, state) {
    delete params.id;
    $.ajax({
        type: 'POST',
        url: "/antique/api/mallProduct/manage/add_product",
        data: params,
        dataType: "json",
        success: function (res) {
            if (res.code == 0) {
                if (state == 1) { //0仓库 1出售中
                    pubilcPro(res.data.id);
                } else {
                    $.toast("上传成功!谢谢您的使用!");
                    window.setTimeout(function () {
                        submiting = false;
                        if(isMiniProgram()){
                            if(isOutStock == 'true'){
                                wx.miniProgram.redirectTo({
                                    url: '/pages/collectList'
                                });
                            } else {
                                wx.miniProgram.navigateBack();
                            }
                        } else {
                            goPreUrl();
                        }
                    }, 1000);
                }
            } else {
                submiting = false;
                $.toast(res.message, "cancel");
            }
        }
    });
}

function upPro(params, state) {
    $.ajax({
        type: 'POST',
        url: "/antique/api/mallProduct/manage/update_product",
        data: params,
        dataType: "json",
        success: function (res) {
            if (res.code == 0) {
                if (state == 1) {
                    pubilcPro(params.id);
                } else {
                    $.toast("修改成功!谢谢您的使用!");
                    window.setTimeout(function () {
                        submiting = false;
                        if(isMiniProgram()){
                            if(isOutStock == 'true'){
                                wx.miniProgram.redirectTo({
                                    url: '/pages/collectList'
                                });
                            } else {
                                wx.miniProgram.navigateBack();
                            }
                        } else {
                            goPreUrl();
                        }
                    }, 1000);
                }
            } else {
                submiting = false;
                $.toast(res.message, "cancel");
            }
        }
    });
}

function pubilcPro(id) {
    var params = {};
    params.id = id;
    params.token = getCookie('token');
    $.ajax({
        type: 'POST',
        url: "/antique/api/mallProduct/manage/publish_product",
        data: params,
        dataType: "json",
        success: function (res) {
            if (res.code == 0) {
                $.toast("操作成功!谢谢您的使用!");
                window.setTimeout(function () {
                    submiting = false;
                    if(isMiniProgram()){
                        if(isOutStock == 'true'){
                            wx.miniProgram.redirectTo({
                                url: '/pages/collectList'
                            });
                        } else {
                            wx.miniProgram.navigateBack();
                        }
                    } else {
                        goPreUrl();
                    }
                }, 1000);
            } else {
                submiting = false;
                $.toast(res.message, "cancel");
            }
        }
    });
}

var salerAgreementDialog = null;
$('#viewAgree').click(function(){
    salerAgreementDialog = $("#salerAgreement").popup();
});

$('#salerAgreement .close').click(function(){
    salerAgreementDialog.closePopup();
});